package com.aracky.system.controller;

import com.aracky.common.annotation.DataDeal;
import com.aracky.common.util.ConvertUtil;
import com.aracky.common.vo.CommonResult;
import com.aracky.common.vo.PageVO;
import com.aracky.system.model.entity.SysTableHeader;
import com.aracky.system.model.entity.SysTableLine;
import com.aracky.system.model.form.SysTableHeaderForm;
import com.aracky.system.model.request.SysTableHeaderRequest;
import com.aracky.system.model.vo.SysTableHeaderVO;
import com.aracky.system.model.vo.SysTableLineVO;
import com.aracky.system.service.SysTableHeaderService;
import com.aracky.system.service.SysTableLineService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 查询表头信息表 前端控制器
 * </p>
 *
 * @author zengzhiping
 * @since 2022-07-18 10:05:43
 */
@RestController
@RequestMapping("/sysTableConfig")
@Api(tags = "数据模型配置相关接口")
public class SysTableHeaderController {

    @Autowired
    private SysTableHeaderService sysTableHeaderService;

    @Autowired
    private SysTableLineService sysTableLineService;


    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "新增或者修改数据模型配置")
    public CommonResult saveOrUpdateTableHeader(@RequestBody SysTableHeaderForm sysTableHeaderForm) {
        sysTableHeaderService.saveOrUpdateTableHeader(sysTableHeaderForm);
        return CommonResult.success("数据模型配置新增成功");
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除数据模型配置")
    @ApiImplicitParam(name = "headIds", value = "表头ids")
    public CommonResult deleteTableHeader(Long[] headIds) {
        sysTableHeaderService.removeBatch(headIds);
        return CommonResult.success("数据模型配置删除成功");
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "查询数据模型分页信息")
    public CommonResult<PageVO<SysTableHeaderVO>> getTableHeaderPage(@RequestBody SysTableHeaderRequest sysTableHeaderRequest) {

        LambdaQueryWrapper<SysTableHeader> headerLambdaQueryWrapper = Wrappers.<SysTableHeader>lambdaQuery()
                .like(StringUtils.isNotBlank(sysTableHeaderRequest.getInstruction()), SysTableHeader::getInstruction, sysTableHeaderRequest.getInstruction())
                .like(StringUtils.isNotBlank(sysTableHeaderRequest.getQueryApi()), SysTableHeader::getQueryApi, sysTableHeaderRequest.getQueryApi())
                .like(StringUtils.isNotBlank(sysTableHeaderRequest.getQureyMethod()), SysTableHeader::getQureyMethod, sysTableHeaderRequest.getQureyMethod())
                .likeRight(StringUtils.isNotBlank(sysTableHeaderRequest.getTableName()), SysTableHeader::getTableName, sysTableHeaderRequest.getTableName());
        Page<SysTableHeader> page = sysTableHeaderService.page(sysTableHeaderRequest.toPage(), headerLambdaQueryWrapper);

        PageVO pageVO = new PageVO<>(page);
        List<SysTableHeaderVO> sysTableHeaderVOList = ConvertUtil.sourceToTargets(page.getRecords(), SysTableHeaderVO.class);
        pageVO.setData(sysTableHeaderVOList);
        return CommonResult.success(pageVO);
    }


    @GetMapping("/getTableLine")
    @ApiOperation(value = "查询数据模型详细数据")
//    @ApiImplicitParam(name = "headId", value = "表头id")
    public CommonResult<SysTableHeaderVO> getTableHeader(Long headId) {
        //查询表头字段
        SysTableHeader sysTableHeader = sysTableHeaderService. getById(headId);
        if (sysTableHeader == null) {
            return CommonResult.success("查询成功");
        }

        //查询表字段
        LambdaQueryWrapper<SysTableLine> lineLambdaQueryWrapper =
                Wrappers.<SysTableLine>lambdaQuery().eq(SysTableLine::getHeadId, sysTableHeader.getHeadId()).orderByAsc(SysTableLine::getSeq);
        List<SysTableLine> lineList = sysTableLineService.list(lineLambdaQueryWrapper);

        //构造返回结果
        SysTableHeaderVO sysTableHeaderVO = ConvertUtil.sourceToTarget(sysTableHeader, SysTableHeaderVO.class);
        List<SysTableLineVO> sysTableLineVOList = ConvertUtil.sourceToTargets(lineList, SysTableLineVO.class);
        sysTableHeaderVO.setSysTableLineList(sysTableLineVOList);
        return CommonResult.success(sysTableHeaderVO);
    }
}
